package com.ds.robot.assistant.job;

import cn.hutool.core.map.MapUtil;
import com.google.common.cache.LoadingCache;
import com.ds.robot.assistant.common.JobHelper;
import com.ds.robot.assistant.component.JsonFileReader;
import com.ds.robot.assistant.service.impl.QxUserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@Component("baseJob")
@Slf4j
public class BaseJob {

    @Autowired
    private JsonFileReader jsonFileReader;

    @Autowired
    private QxUserService qxUserService;

    @Resource
    private LoadingCache<String, String> qxUserCache;

    protected void appendUserId(String personName, StringBuilder builder, Logger log) {
        List<String> nameList = JobHelper.extractNameFromStr(personName);
        appendUserIdFromCache(nameList, builder, true, log);
    }

    protected void appendUserIdOneLine(String personName, StringBuilder builder, Logger log) {
        List<String> nameList = JobHelper.extractNameFromStr(personName);
        appendUserIdFromCache(nameList, builder, false, log);
    }

    protected void appendUserId(List<String> nameList, StringBuilder builder, boolean isNewLine, Logger log) {
        if (!nameList.isEmpty()) {
            Map<String, String> nameIdMap = getUserNameToIdMap();
            if (isNewLine) {
                builder.append("\n\n ");
            }
            for (String name : nameList) {
                if (nameIdMap.containsKey(name)) {
                    builder.append("<@").append(nameIdMap.get(name)).append("> ");
                } else {
                    log.warn("最终未找到用户：{}", name);
                }
            }
        }
    }

    protected void appendUserIdFromCache(List<String> nameList, StringBuilder builder, boolean isNewLine, Logger log) {
        if (!nameList.isEmpty()) {
            if (isNewLine) {
                builder.append("\n\n ");
            }
            for (String name : nameList) {
                String userId = null;
                try {
                    userId = qxUserCache.get(name);
                } catch (Exception e) {
                    log.error("缓存中未找到用户：{}", name, e);
                }
                if (StringUtils.hasText(userId)) {
                    builder.append("<@").append(userId).append("> ");
                } else {
                    log.warn("最终未找到用户：{}", name);
                }
            }
        }
    }

    public Map<String, String> getUserNameToIdMap() {
        Map<String, String> nameIdMap = qxUserService.getUserMap();
        if (MapUtil.isEmpty(nameIdMap)) {
            log.warn("=====Mongo查询为空");
            nameIdMap = jsonFileReader.getUserName2IdMap();
        }
        return nameIdMap;
    }

    public String getNoticeStr(String userId) {
        return "<@" + userId + ">";
    }

}
